package com.itheima.test;

import com.alibaba.excel.EasyExcel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringBootTest
@RunWith(SpringRunner.class)
public class TextExcel {

    // linstener 对象不能被Spring管理,每次读取excel都必须是新的实例
    @Test
    public void test(){
        //读取文件
        EasyExcel.read(new File("E:\\lianxi\\ceshi\\部分数据.xls"),
                TbClueExcelVo.class,
                new ClueDataListener()).sheet().doRead(); //sheet 页签
    }

    @Autowired
    private  TBClueService tbClueService ;

    // 方式一 通过file对象 读取excel文件
    @Test
    public void test1(){
        ClueDataListener2 listener2 =  new ClueDataListener2(tbClueService);
        //读取文件
        EasyExcel.read(new File("E:\\lianxi\\ceshi\\部分数据.xls"),
                TbClueExcelVo.class,
                listener2).
                sheet().
                doRead(); //sheet 页签

        Map result = listener2.getResult();
        System.out.println(result);
    }

    // 方式二 通过流读取 excel文件
    @Test
    public void test2() throws FileNotFoundException {
        ClueDataListener2 listener2 =  new ClueDataListener2(tbClueService);


        FileInputStream InputStream = new FileInputStream(new File("E:\\lianxi\\ceshi\\部分数据.xls"));

        //读取文件
        EasyExcel.read(InputStream,
                        TbClueExcelVo.class,
                        listener2).
                        sheet().
                        doRead(); //sheet 页签

        Map result = listener2.getResult();
        System.out.println(result);
    }
     // 写excel的例子
    @Test
    public void testWrite(){
        List<TbClueExcelVo> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TbClueExcelVo vo = new TbClueExcelVo();
            vo.setPhone("15538534365");
            vo.setName("绿子");
            list.add(vo);
        }
        EasyExcel.write(new File("E:\\lianxi\\ceshi\\test.xls")
                ,TbClueExcelVo.class)
                .sheet("seet名字")
                .doWrite(list);
    }
}
